package com.woniu.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.entity.PageData;
import com.woniu.entity.Result;
import com.woniu.entity.dto.CollectDTO;
import com.woniu.entity.vo.CollectVO;
import com.woniu.pojo.Collect;
import com.woniu.pojo.Training;
import com.woniu.pojo.User;
import com.woniu.service.ICollectService;
import com.woniu.service.ITrainingService;
import com.woniu.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiaosong
 * @since 2023-04-01
 */
@RestController
@RequestMapping("/collect")
@Slf4j
@Api(tags = "收藏表管理")
public class CollectController extends BaseController{

    @Autowired
    private ICollectService collectService;

    @Autowired
    private IUserService userService;

    @Autowired
    private ITrainingService trainingService;

    /*查询收藏表并分页*/
    @PostMapping("/searchPage/{currentPage}/{pageSize}")
    @ApiOperation("分页查询")
    public Result searchPage(@PathVariable Integer currentPage,
                             @PathVariable Integer pageSize,
                             @RequestBody CollectDTO collectDTO){
        PageData pageData = collectService.searchPage(currentPage,pageSize,collectDTO);
        return toDataResult(pageData);
    }

    /*新增收藏*/
    @PostMapping("/insert")
    @ApiOperation("新增收藏")
    public Result increase(@RequestBody Collect collect){

        //若新增的userid在user表中不存在 返回false
        User oneUser = userService.getById(collect.getUserId());
        if (oneUser == null){
            return toResult(false);
        }
        //若新增的trainingid在training表中不存在 返回false
        Training oneTraining = trainingService.getById(collect.getTrainingId());
        if (oneTraining == null){
            return toResult(false);
        }

        //若新增的userid与trainingid在收藏表中已经存在 返回false
        QueryWrapper<Collect> queryWrapperCollect = new QueryWrapper<>();
        queryWrapperCollect.eq("user_id",collect.getUserId());
        queryWrapperCollect.eq("training_id",collect.getTrainingId());
        List<Collect> collectList = collectService.list(queryWrapperCollect);
        for (Collect oneCollect: collectList){
            if (oneCollect != null){
                return toResult(false);
            }
        }

        Boolean flag = collectService.increase(collect);
        return toResult(flag);
    }

    @PostMapping("/update")
    @ApiOperation("修改收藏")
    public Result revise(@RequestBody Collect collect){

        //若修改的userid与数据库中的userid不一致 返回false
        QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",collect.getUserId());
        List<Collect> collectList = collectService.list(queryWrapper);
        for (Collect oneCollect :collectList){
            if (!oneCollect.getUserId().equals(collect.getUserId())){
                return toResult(false);
            }
        }


        //若修改的trainingid在training表中不存在 返回false
        Training oneTraining = trainingService.getById(collect.getTrainingId());
        if (oneTraining == null){
            return toResult(false);
        }

        //若修改的userid与trainingid在收藏表中已经存在 返回false
        QueryWrapper<Collect> queryWrapperCollect = new QueryWrapper<>();
        queryWrapperCollect.eq("user_id",collect.getUserId());
        queryWrapperCollect.eq("training_id",collect.getTrainingId());
        Collect oneCollect = collectService.getOne(queryWrapperCollect);
        if (oneCollect != null){
            return toResult(false);
        }

        Boolean flag = collectService.revise(collect);
        return toResult(flag);
    }

    /*单个删除收藏*/
    @PostMapping("/deleteById/{id}")
    @ApiOperation("删除收藏")
    public Result decrease(@PathVariable Integer id){

        Boolean flag = collectService.decrease(id);
        return toResult(flag);

    }

    /*批量删除收藏*/
    @PostMapping("/deletePart")
    @ApiOperation("批量删除收藏")
    public Result deletePart(@RequestBody List<Integer> ids){
        Boolean flag = collectService.deletePart(ids);
        return toResult(flag);
    }

    //修改数据所需的按id查询
    @GetMapping("/searchById/{id}")
    @ApiOperation("按id查询")
    public Result searchById(@PathVariable Integer id){

        Collect collect = collectService.searchById(id);
        return toDataResult(collect);
    }

    @ApiOperation("导出数据")
    @PostMapping("/export")
    public void exportExcel(@RequestBody(required = false) CollectDTO collectDTO,
                           HttpServletResponse response) throws IOException {
        PageData pageData =  collectService.searchPage(1, 1000, collectDTO);
        List<Collect> collectList = (List<Collect>) pageData.getData();
        EasyExcel.write(response.getOutputStream(), CollectVO.class).sheet().doWrite(collectList);
    }
}
